﻿Namespace form

    Public Class ListFormBase
#Region "构造函数"

        Public Sub New()

            ' 此调用是 Windows 窗体设计器所必需的。
            InitializeComponent()

            ' 在 InitializeComponent() 调用之后添加任何初始化。

        End Sub

        Public Sub New(ByVal 菜单数据 As DataTable)

            ' 此调用是 Windows 窗体设计器所必需的。
            InitializeComponent()

            ' 在 InitializeComponent() 调用之后添加任何初始化。
            Me.菜单数据 = 菜单数据
        End Sub

        Public Sub New(ByVal 菜单数据 As DataTable, ByVal 菜单图标 As ImageList)

            ' 此调用是 Windows 窗体设计器所必需的。
            InitializeComponent()

            ' 在 InitializeComponent() 调用之后添加任何初始化。
            Me.菜单数据 = 菜单数据
            Me.菜单图标 = 菜单图标

        End Sub

#End Region

#Region "属性"

        Private _菜单数据 As DataTable
        Public Property 菜单数据() As DataTable
            Get
                Return _菜单数据
            End Get
            Set(ByVal value As DataTable)
                _菜单数据 = value
            End Set
        End Property

        Private _菜单图标 As ImageList
        Public Property 菜单图标() As ImageList
            Get
                Return _菜单图标
            End Get
            Set(ByVal value As ImageList)
                _菜单图标 = value
            End Set
        End Property

        Private _添加没有数据节点 As Boolean = True
        Public Property 添加没有数据节点() As Boolean
            Get
                Return _添加没有数据节点
            End Get
            Set(ByVal value As Boolean)
                _添加没有数据节点 = value
            End Set
        End Property
#Region "返回值"

        Private _编号 As String = ""
        Public Property 编号() As String
            Get
                Return _编号
            End Get
            Set(ByVal value As String)
                _编号 = value
            End Set
        End Property

        Private _标题 As String = ""
        Public Property 标题() As String
            Get
                Return _标题
            End Get
            Set(ByVal value As String)
                _标题 = value
            End Set
        End Property
#End Region

#End Region

#Region "事件"

        Public Overridable Sub 加载菜单前()

        End Sub

        Public Overridable Sub 加载菜单后()

        End Sub

        Private Sub MenuBase_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            刷新菜单()

        End Sub

        Public Overridable Sub 刷新菜单()

            加载菜单前()

            加载菜单()

            加载菜单后()

        End Sub

        '编号 标题        父编号     图片Key
        '1    工具        0         
        '2    工具子目录1 1                                
        '3    工具子目录2 1
        '4    工具子目录3 1
        '5    工具子目录4 1        
        Public Sub 加载菜单()
            tvMenu.Nodes.Clear()

            Dim nd As TreeNode

            If 添加没有数据节点 = True Then
                nd = tvMenu.Nodes.Add("不选择")
                nd.Name = 0
            End If

            If _菜单数据 Is Nothing OrElse _菜单数据.Rows.Count = 0 Then
                Exit Sub
            End If

            If 菜单图标 Is Nothing OrElse 菜单图标.Images.Count = 0 Then
                菜单图标 = New ImageList
                菜单图标.ImageSize = New Size(16, 16)
                菜单图标.Images.Add("Open", My.Resources.folder_blue_icon)
                菜单图标.Images.Add("Close", My.Resources.folder_close_blue_icon)
            End If

            tvMenu.ImageList = 菜单图标
            Dim drs() As DataRow = _菜单数据.Select("父编号=0")

            For index As Integer = 0 To drs.Length - 1
                If 是否添加到窗体(drs(index)) Then
                    nd = tvMenu.Nodes.Add(CStr(drs(index)("标题")))
                    nd.Name = CStr(drs(index)("编号"))
                    nd.SelectedImageKey = CStr(drs(index)("图片Key"))
                    加载菜单(nd)
                End If
            Next

        End Sub

        Public Overridable Function 是否添加到窗体(ByVal dr As DataRow) As Boolean
            Return True
        End Function

        Private Sub 加载菜单(ByVal parentNode As TreeNode)

            Dim drs() As DataRow = _菜单数据.Select("父编号=" & parentNode.Name)
            Dim nd As TreeNode
            For index As Integer = 0 To drs.Length - 1
                If 是否添加到窗体(drs(index)) Then
                    nd = parentNode.Nodes.Add(CStr(drs(index)("标题")))
                    nd.Name = CStr(drs(index)("编号"))
                    nd.SelectedImageKey = CStr(drs(index)("图片Key"))
                    加载菜单(nd)
                End If
            Next

        End Sub
#End Region

#Region "共通方法"

        Public Shared Function GetEmptyMenuTable() As DataTable

            Dim dtMenu As New DataTable

            Dim dc As DataColumn = dtMenu.Columns.Add("编号", GetType(Integer))
            dc.DefaultValue = 0

            dc = dtMenu.Columns.Add("标题", GetType(String))
            dc.DefaultValue = ""

            dc = dtMenu.Columns.Add("父编号", GetType(Integer))
            dc.DefaultValue = 0

            dc = dtMenu.Columns.Add("图片Key", GetType(String))
            dc.DefaultValue = "Open"

            Return dtMenu

        End Function

#End Region

        Public Overridable Sub NodeMouseDoubleClickImpl(ByVal nd As TreeNode)

            Dim 编号V As Integer = nd.Name
            Dim drs() As DataRow = Me.菜单数据.Select("编号=" & 编号V)

            If drs.Length = 0 Then
                If nd.Name = "0" Then
                    编号 = "0"
                    标题 = ""
                End If
            Else
                编号 = CStr(drs(0)("编号")).Trim
                标题 = CStr(drs(0)("标题")).Trim
            End If

            DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()

        End Sub

        '重写次类填充类数据
        Public Overridable Sub FormShowBefore(ByRef frm As Windows.Forms.Form)

        End Sub

        Private Sub tvMenu_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvMenu.NodeMouseDoubleClick

            NodeMouseDoubleClickImpl(e.Node)

        End Sub
    End Class
End Namespace

